package com.huahai.controller;

import com.huahai.entity.Order;
import com.huahai.entity.Product;
import com.huahai.service.IOrderService;
import com.huahai.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
//其作用就是局部的动态刷新配置文件，当配置中心出现配置更新之后，
// 在需要刷新配置文件的刷新点上添加上注解就可以读取到最新的配置中心的配置文件了**
//@RefreshScope
public class OrderController {

/*
    //从nacos配置中心读取配置
    @Value("${config.info}")
    private String config;
*/

    @Autowired
    IOrderService orderService;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    DiscoveryClient discoveryClient;


    @Autowired
    IProductService productService;


    //使用feign
    @GetMapping("/order/{pid}")
    public Order order(@PathVariable("pid") int id) {
        Order order = new Order();

        //通过fegin调用商品微服务
        Product product = productService.findById(id);

        order.setUid(1);
        order.setNumber(99);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setUsername("liyang");

        return order;
    }


    //Ribbon负载均衡
    /*@GetMapping("/order/{pid}")
    public Order order(@PathVariable("pid") int id) {
        Order order = new Order();
        //获取该服务的所有实例
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");

        String url = "service-product";

        //问题
        //1 拼接字符串方式，可读性差
        //2 代码风格不一致
        Product product =
                restTemplate.getForObject("http://" + url + "/product/" + id, Product.class);
        //order.setOid(1l);
        order.setUid(1);
        order.setNumber(99);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setUsername("liyang");

        return order;
    }*/
    //自定义负载均衡
    /*@GetMapping("/order/{pid}")
    public Order order(@PathVariable("pid") int id) {
        Order order = new Order();
        //获取该服务的所有实例
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        //实现随机访问方式的负载均衡
        int index = new Random().nextInt(instances.size());
        ServiceInstance instance = instances.get(index);

        String url = instance.getHost() + ":" + instance.getPort();
        Product product =
                restTemplate.getForObject("http://" + url + "/product/" + id, Product.class);
        //order.setOid(1l);
        order.setUid(1);
        order.setNumber(99);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setUsername("liyang");

        return order;
    }*/
    //使用nacos
    /* @GetMapping("/order/{pid}")
    public Order order(@PathVariable("pid") int id) {


        Order order = new Order();
        //获取该服务的所有实例
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);

        String url = instance.getHost() + ":" + instance.getPort();
        Product product =
                restTemplate.getForObject("http://" + url + "/product/" + id, Product.class);
        //order.setOid(1l);
        order.setUid(1);
        order.setNumber(99);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setUsername("liyang");

        return order;
    }
*/
    //原始方式调用
    /*@GetMapping("/order/{pid}")
    public Order order(@PathVariable("pid") int id){


        Order order = new Order();
        //问题：
        //1、一旦服务提供者的信息发生变化，服务调用者的代码就不得不改
        //2、一旦服务提供者做了集群，服务调用者就无法实现负载均衡
        //3、一旦服务变得越来越多，如何管理服务清单也是问题
        Product product =
                restTemplate.getForObject("http://localhost:8081/product/" + id, Product.class);
        //order.setOid(1l);
        order.setUid(1);
        order.setNumber(99);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setUsername("liyang");

        int save = orderService.save(order);
        if (save == 1){
            return order;
        }else {
            return null;
        }
    }*/
}
